package com.dxy.wx.controller;

import com.dxy.bean.Courier;
import com.dxy.bean.Message;
import com.dxy.bean.User;
import com.dxy.mvc.ResponseBody;
import com.dxy.service.CourieService;
import com.dxy.service.UserService;
import com.dxy.service.impl.CourieServiceImpl;
import com.dxy.service.impl.UserServiceImpl;
import com.dxy.utils.JSONUtil;
import com.dxy.utils.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author DELL
 */

public class RegisteredController {
    UserService userService = new UserServiceImpl();
    CourieService courieService = new CourieServiceImpl();
    @ResponseBody("/wx/reg.do")
    public String registered(HttpServletRequest request, HttpServletResponse response) {
        //1.    接收请求的参数
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String password = request.getParameter("password1");
        String uPhone = userService.findUserPhone(userPhone);
        String cPhone = courieService.findCouierPhone(userPhone);
        //  查询输入的手机号是否存在于数据库
        Message msg = new Message();
        if (userPhone!=uPhone && userPhone!=cPhone){
            User u = new User();
            u.setUsername(username);
            u.setUserPhone(userPhone);
            u.setPassword(password);
            //2.    将接收到的用户名 手机号码 密码 调用service 进行注册
            UserService userService = new UserServiceImpl();
            boolean flag = userService.insert(u);
            //3.    判断注册的结果, 并根据结果准备不同的消息对象

            if (flag) {
                msg.setStatus(0);
                msg.setResult("注册成功");
            } else {
                msg.setStatus(-1);
                msg.setResult("用户已存在");
            }
        }


        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/wx/wxUpdate.do")
    public String updateUser(HttpServletRequest request, HttpServletResponse response){
        UserService userService = new UserServiceImpl();
        CourieService courieService = new CourieServiceImpl();
        User user = UserUtil.getWxUser(request.getSession());
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String icCard = request.getParameter("icCard");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String sysCode ="123456";
                // UserUtil.getLoginSms(request.getSession(),userPhone);
        Message msg = new Message();
        if (sysCode==null){
            //这个手机号码未获取短信
            msg.setStatus(-1);
            msg.setResult("手机号码未获取短信验证");
        } else if (sysCode.equals(code)) {
            //验证码一致
            msg.setStatus(0);
            // 修改后的用户对象
            // 判断当前用户是普通用户还是快递员
            boolean flag;
            // 用户认证
            if(userPhone.equals(user.getUserPhone())){
                User newUser = new User();
                newUser.setUsername(username);
                newUser.setUserPhone(userPhone);
                newUser.setIcCard(icCard);
                newUser.setPassword(password);
                 flag = userService.wxInfoUpdate(userPhone,newUser);
            }
            //快递员认证
            else {
                Courier newUser = new Courier();
                newUser.setUsername(username);
                newUser.setUserPhone(userPhone);
                newUser.setIcCard(icCard);
                newUser.setPassword(password);
                flag = courieService.wxInfoUpdate(userPhone,newUser);
            }

            System.out.println(flag);
            if (flag){
                msg.setStatus(1);
                user.setUser(true);
                msg.setResult("认证成功");
            }else {
                msg.setStatus(-1);
                msg.setResult("认证失败");
            }

        }else {
            //验证码不一致
            msg.setStatus(-2);
            msg.setResult("验证码不一致,请检查");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }



}
